package com.nw.news.ui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.nw.news.R;
import com.nw.news.bean.Notice;
import com.nw.news.ui.notice.NoticeActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.yuan.base.framework.BaseFrameworkFragment;
import com.yuan.base.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页通告
 */
public class MainMenuFragment extends BaseFrameworkFragment implements OnBannerListener, View.OnClickListener {

	// --------------------------控件--------------------------
	private Banner banner;
	// --------------------------变量--------------------------

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_main_menu;
	}

	@Override
	protected void bindViews() {
		banner = findViewById(R.id.banner1);
		setOnClickListener(R.id.tv_to_notice, v -> {
			toNotice();
		});
		setOnClickListener(R.id.temp1, v -> toNotice());
		setOnClickListener(R.id.temp2, v -> toNotice());
		setOnClickListener(R.id.temp3, v -> toNotice());
		setOnClickListener(R.id.temp4, v -> toNotice());
		setOnClickListener(R.id.temp5, v -> toNotice());
		setOnClickListener(R.id.temp6, v -> toNotice());
		setOnClickListener(R.id.temp7, v -> toNotice());
		setOnClickListener(R.id.temp8, v -> toNotice());
		setOnClickListener(R.id.temp9, v -> toNotice());
		setOnClickListener(R.id.temp10, v -> toNotice());
		setOnClickListener(R.id.temp11, v -> toNotice());
	}

	public void toNotice() {
		Intent intent = new Intent(context, NoticeActivity.class);
		intent.putExtra("type", 0);
		startActivity(intent);
	}

	@Override
	protected void initUI() {
		setBanner();
	}


	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onResume() {
		super.onResume();
	}


	@Override
	public void onStart() {
		super.onStart();
	}

	private void setBanner() {
		banner = findViewById(R.id.banner1);
		List<Integer> imgs = new ArrayList<>();
		imgs.add(R.drawable.p_head);
		imgs.add(R.drawable.temp4);
		imgs.add(R.drawable.temp5);
		List<String> titles = new ArrayList<>();
		titles.add("激动人心！NASA公布“好奇号”最新自拍照");
		titles.add("贵飞FTC—2000G多用途飞机02架机首飞成功");
		titles.add("探访特斯拉上海工厂 ：生产线进行最后调试 招工仍在进行中");
		//设置内置样式，共有六种可以点入方法内逐一体验使用。
		banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
		//设置图片加载器，图片加载器在下方
		banner.setImageLoader(new ImageLoader() {
			@Override
			public void displayImage(Context context, Object path, ImageView imageView) {
				imageView.setImageResource((int) path);
			}
		});
		//设置图片网址或地址的集合
		banner.setImages(imgs);
		//设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
		banner.setBannerAnimation(Transformer.Default);
		//设置轮播图的标题集合
		banner.setBannerTitles(titles);
		//设置轮播间隔时间
		banner.setDelayTime(3000);
		//设置是否为自动轮播，默认是“是”。
		banner.isAutoPlay(true);
		//设置指示器的位置，小点点，左中右。
		banner.setIndicatorGravity(BannerConfig.CENTER)
				.setOnBannerListener(this)//以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
				.start();//必须最后调用的方法，启动轮播图。
	}

	@Override
	public void OnBannerClick(int position) {
		Intent intent = new Intent(getContext(), H5Activity.class);
		switch (position) {
			case 0:
				Notice notice3 = new Notice();
				notice3.setTitle("激动人心！NASA公布“好奇号”最新自拍照");
				notice3.setPublisher("网易新闻");
				notice3.setPublishDate(DateUtil.stringToDate("2019-10-30 11:50:41"));
				notice3.setContent("https://3g.163.com/news/photoview/0001/2304923.html");
				notice3.setSchoolId(R.drawable.p_head);
				intent.putExtra("notice", notice3);
				startActivity(intent);
				break;
			case 1:
				Notice notice4 = new Notice();
				notice4.setTitle("贵飞FTC—2000G多用途飞机02架机首飞成功");
				notice4.setPublisher("网易新闻");
				notice4.setPublishDate(DateUtil.stringToDate("2019-10-29 21:50:41"));
				notice4.setContent("https://3g.163.com/war/photoview/0001/2304930.html");
				notice4.setSchoolId(R.drawable.temp4);
				intent.putExtra("notice", notice4);
				startActivity(intent);
				break;
			case 2:
				Notice notice5 = new Notice();
				notice5.setTitle("探访特斯拉上海工厂 ：生产线进行最后调试 招工仍在进行中");
				notice5.setPublisher("腾讯新闻");
				notice5.setPublishDate(DateUtil.stringToDate("2019-10-29 15:50:41"));
				notice5.setContent("https://xw.qq.com/cmsid/20191030A0C8MP00?f=newdc");
				notice5.setSchoolId(R.drawable.temp5);
				intent.putExtra("notice", notice5);

				startActivity(intent);
				break;
		}
		Log.i("tag", "你点了第" + position + "张轮播图");
	}

	@Override
	public void onClick(View v) {
		toNotice();
	}
}
